package com.collectplus.express.passport;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.collectplus.express.R;
import com.collectplus.express.model.Const;
import com.collectplus.express.model.PassportBean;
import com.collectplus.express.view.ViewGroupAdapter;

import java.util.ArrayList;

import droid.frame.utils.lang.Str;

/**
 * 包裹状态列表
 *
 * @author HuXinDan<br>
 *         2016-10-5下午3:06:06
 */
public class PassportAdapter extends ViewGroupAdapter<PassportBean> {

    private Context context;
    //    private int[] backColors = {R.drawable.passport_corners_top_1, // 上门(标准+有袋)
    //            R.drawable.passport_corners_top_2, // 到柜
    //            R.drawable.passport_corners_top_3};// 到驿站

    private int[] backColors = {R.drawable.passport_corners_top_3, R.drawable.passport_corners_top_4};

    public PassportAdapter(ArrayList<PassportBean> items, ViewGroup viewGroup) {
        super(items, viewGroup);
        this.context = viewGroup.getContext();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.passport_item, parent, false);
            holder.logo = (ImageView) convertView.findViewById(R.id.passport_logo);
            holder.send1 = (TextView) convertView.findViewById(R.id.send_code1);
            holder.send2 = (TextView) convertView.findViewById(R.id.send_code2);
            holder.recipients1 = (TextView) convertView.findViewById(R.id.parcel_recipients1);
            holder.recipients2 = (TextView) convertView.findViewById(R.id.parcel_recipients2);
            holder.codeItem = convertView.findViewById(R.id.code_item);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        PassportBean item = getItem(position);
        holder.send1.setText("寄件码");
        if (item.isTake()) {
            holder.recipients2.setText(item.getStationName());// 收件人
        } else {
            holder.recipients2.setText(item.getReceiverName());// 收件人
        }
        if (Str.isEmpty(item.getCheckCode())) {
            holder.send2.setText("000000");
            holder.send2.setVisibility(View.INVISIBLE); // 设置不可见、主要为了控制宽度，使上面的"寄件码"文本对齐
        } else {
            holder.send2.setText(item.getCheckCode());
            holder.send2.setVisibility(View.VISIBLE);
        }
        if (item.getPassportStatus() != 1) {
            holder.codeItem.setVisibility(View.INVISIBLE);
        }

        // 显示不同的图片背景
        int backgroudColor = 0;
        if (item.isCityWide() || item.isTake()) {
            backgroudColor = backColors[0];
        } else {
            //        if (item.getIsStation()) {
            //            backgroudColor = backColors[2];
            //        } else if (item.getIsBox()) {
            //            backgroudColor = backColors[1];
            //        } else {
            backgroudColor = backColors[1];
        }
        //        }
        convertView.setBackgroundResource(backgroudColor);

        if (item.getParcelType() == Const.ParcelType.standard//
                || item.getParcelType() == Const.ParcelType.bagToBox //
                || item.getParcelType() == Const.ParcelType.bagToHome) {
            holder.logo.setImageResource(R.drawable.passport_top_logo);
            int color1 = context.getResources().getColor(R.color.parcel_list_brown);
            holder.send1.setTextColor(color1);
            holder.send2.setTextColor(color1);
            holder.recipients1.setTextColor(color1);
            holder.recipients2.setTextColor(color1);
        } else {
            if (item.isCityWide() || item.isTake()) {
                if (item.isTake()) {
                    if (item.getTakeStatus() == 11 || item.getTakeStatus() == 12) {
                        holder.recipients1.setText(item.getTakeTime() + "  取件成功");
                    } else {
                        holder.recipients1.setText(item.getExpressCompanyName());
                    }
                    holder.logo.setImageResource(R.drawable.passport_top_logo_black_take);
                } else {
                    holder.logo.setImageResource(R.drawable.passport_top_logo_black_samecity_send);
                    holder.recipients1.setText(item.getUserBuildName());// 收件人城市
                }
                int color2 = context.getResources().getColor(R.color.app_fontcolor_333333);
                holder.send1.setTextColor(color2);
                holder.send2.setTextColor(color2);
                holder.recipients1.setTextColor(color2);
                holder.recipients2.setTextColor(color2);
            } else {
                holder.recipients1.setText(item.getReceiverCity());// 收件人城市
                //            if (item.getIsBox()) {
                holder.logo.setImageResource(R.drawable.passport_top_logo_white);
                int color2 = context.getResources().getColor(R.color.white);
                holder.send1.setTextColor(color2);
                holder.send2.setTextColor(color2);
                holder.recipients1.setTextColor(color2);
                holder.recipients2.setTextColor(color2);
                //            } else {
                //                holder.logo.setImageResource(R.drawable.passport_top_logo_black);
                //                int color2 = context.getResources().getColor(R.color.app_fontcolor_333333);
                //                holder.send1.setTextColor(color2);
                //                holder.send2.setTextColor(color2);
                //                holder.recipients1.setTextColor(color2);
                //                holder.recipients2.setTextColor(color2);
                //            }
            }

        }
        return convertView;
    }

    private class ViewHolder {
        ImageView logo;
        View codeItem;
        TextView send1, send2, recipients1, recipients2;
    }

}
